package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateUserOrderPay
//@description: 创建UserOrderPay记录
//@param: userOrderPay model.UserOrderPay
//@return: err error

func CreateUserOrderPay(userOrderPay model.UserOrderPay) (err error) {
	err = global.GVA_DB.Create(&userOrderPay).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteUserOrderPay
//@description: 删除UserOrderPay记录
//@param: userOrderPay model.UserOrderPay
//@return: err error

func DeleteUserOrderPay(userOrderPay model.UserOrderPay) (err error) {
	err = global.GVA_DB.Delete(&userOrderPay).Error
	return err
}
func DeleteUserOrderPayByOrderID(orderId int)(err error){
	err = global.GVA_DB.Delete(&[]model.UserOrderPay{},"order_id = ?",orderId).Error
	return err
}
//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteUserOrderPayByIds
//@description: 批量删除UserOrderPay记录
//@param: ids request.IdsReq
//@return: err error

func DeleteUserOrderPayByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.UserOrderPay{},"id in ?",ids.Ids).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateUserOrderPay
//@description: 更新UserOrderPay记录
//@param: userOrderPay *model.UserOrderPay
//@return: err error

func UpdateUserOrderPay(userOrderPay model.UserOrderPay) (err error) {
	err = global.GVA_DB.Save(&userOrderPay).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetUserOrderPay
//@description: 根据id获取UserOrderPay记录
//@param: id uint
//@return: err error, userOrderPay model.UserOrderPay

func GetUserOrderPay(id uint) (err error, userOrderPay model.UserOrderPay) {
	err = global.GVA_DB.Where("id = ?", id).Preload(`SysUser`).First(&userOrderPay).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetUserOrderPayInfoList
//@description: 分页获取UserOrderPay记录
//@param: info request.UserOrderPaySearch
//@return: err error, list interface{}, total int64

func GetUserOrderPayInfoList(info request.UserOrderPaySearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&model.UserOrderPay{})
    var userOrderPays []model.UserOrderPay
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.DoneUser != 0 {
        db = db.Where("`done_user` = ?",info.DoneUser)
    }
    if info.GetName != "" {
        db = db.Where("`get_name` = ?",info.GetName)
    }
    if info.Number != 0 {
        db = db.Where("`number` = ?",info.Number)
    }
    if info.OrderId != 0 {
        db = db.Where("`order_id` = ?",info.OrderId)
    }
    if info.PayName != "" {
        db = db.Where("`pay_name` = ?",info.PayName)
    }
    if info.PayTime != "" {
        db = db.Where("`pay_time` = ?",info.PayTime)
    }
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Preload(`SysUser`).Find(&userOrderPays).Error
	return err, userOrderPays, total
}